package com.ruoyi.system.service.impl;

import java.util.Arrays;
import java.util.List;

import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.system.domain.vo.AddWeixinUserVo;
import com.ruoyi.system.service.ISysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.SysWxUserMapper;
import com.ruoyi.system.domain.SysWxUser;
import com.ruoyi.system.service.ISysWxUserService;
import org.springframework.util.CollectionUtils;

/**
 * 小程序-微信openid用户Service业务层处理
 * 
 * @author rey
 * @date 2022-10-26
 */
@Service
public class SysWxUserServiceImpl implements ISysWxUserService 
{
    @Autowired
    private SysWxUserMapper sysWxUserMapper;
    @Autowired
    private ISysUserService userService;

    /**
     * 查询小程序-微信openid用户
     * 
     * @param id 小程序-微信openid用户主键
     * @return 小程序-微信openid用户
     */
    @Override
    public SysWxUser selectSysWxUserById(String id)
    {
        return sysWxUserMapper.selectSysWxUserById(id);
    }

    /**
     * 查询小程序-微信openid用户列表
     * 
     * @param sysWxUser 小程序-微信openid用户
     * @return 小程序-微信openid用户
     */
    @Override
    public List<SysWxUser> selectSysWxUserList(SysWxUser sysWxUser)
    {
        return sysWxUserMapper.selectSysWxUserList(sysWxUser);
    }

    /**
     * 新增小程序-微信openid用户
     * 
     * @param sysWxUser 小程序-微信openid用户
     * @return 结果
     */
    @Override
    public int insertSysWxUser(SysWxUser sysWxUser)
    {
        return sysWxUserMapper.insertSysWxUser(sysWxUser);
    }

    /**
     * 修改小程序-微信openid用户
     * 
     * @param sysWxUser 小程序-微信openid用户
     * @return 结果
     */
    @Override
    public int updateSysWxUser(SysWxUser sysWxUser)
    {
        return sysWxUserMapper.updateSysWxUser(sysWxUser);
    }

    /**
     * 批量删除小程序-微信openid用户
     * 
     * @param ids 需要删除的小程序-微信openid用户主键
     * @return 结果
     */
    @Override
    public int deleteSysWxUserByIds(String[] ids)
    {
        return sysWxUserMapper.deleteSysWxUserByIds(ids);
    }

    /**
     * 删除小程序-微信openid用户信息
     * 
     * @param id 小程序-微信openid用户主键
     * @return 结果
     */
    @Override
    public int deleteSysWxUserById(String id)
    {
        return sysWxUserMapper.deleteSysWxUserById(id);
    }

    @Override
    public SysWxUser bindWeixinUser(AddWeixinUserVo userVo) {
        SysWxUser param = new SysWxUser();
        param.setOpenid(userVo.getOpenid());
        List<SysWxUser> sysWxUsers = this.sysWxUserMapper.selectSysWxUserList(param);
        if(CollectionUtils.isEmpty(sysWxUsers)){
            //如果没有找到对应的数据创建用户相关信息
            SysUser sysUser = new SysUser();
            //写死科室
            sysUser.setDeptId(200L);
            sysUser.setNickName(userVo.getNickname());
            sysUser.setUserName(userVo.getOpenid());
            sysUser.setAvatar(userVo.getAvatar());
            //写死密码
            sysUser.setPassword(SecurityUtils.encryptPassword("123456"));
            sysUser.setStatus("0");
            sysUser.setDelFlag("0");
            //写死权限
            sysUser.setRoleIds(new Long []{200L});
            sysUser.setUserType("01");
            userService.insertUser(sysUser);
            userVo.setUserId(sysUser.getUserId());
            this.insertSysWxUser(userVo);
            return userVo;
        }else {
            return sysWxUsers.get(0);
        }
    }
}
